# 队列调度
# python3.6.5
import queue
import time
from multiprocessing.managers import BaseManager as Manager
import threading


def log(msg):
	print(f'[{time.strftime("%Y-%d-%m %H:%M:%S")}]: {msg}')


task_queue = queue.Queue()
task_queue2 = queue.Queue()


def get_task_queue():
	return task_queue

def get_task_queue2():
	return task_queue2


# 多任务添加队列
def put_master_queue(manager, queue_key, task_callback):
	# 填充队列
	# task = manager.get_task_queue()
	task = getattr(manager, queue_key)()
	while 1:
		if task.empty():
			# 生成任务列表
			task_list = task_callback()
			for t in task_list:
				task.put(t)
				log(f'[{queue_key}]添加任务:{t}')
		else:
			log(f'[{queue_key}]任务剩余: {task.qsize()}')
			time.sleep(10)


def get_task_list():
	return range(5)


def get_task_list2():
	return range(15, 20)


queue_items = {
	'get_task_queue': get_task_list,
	'get_task_queue2': get_task_list2,
}


# 多线程添加任务
def thread_call_func(manager, queue_items):
	ts = []
	for queue_key, task_callback in queue_items.items():
		t = threading.Thread(target=put_master_queue, args=(manager, queue_key, task_callback,))
		t.setDaemon(True)
		ts.append(t)

	for t in ts:
		t.start()

	for t in ts:
		t.join()


def task_master(address=('127.0.0.1', 5000), authkey=b'aabbcc'):
	# 创建生产队列,注册到服务中
	
	for queue_key in queue_items.keys():
		Manager.register(queue_key, callable=globals()[queue_key])

	manager = Manager(address=address, authkey=authkey)
	manager.start()

	thread_call_func(manager, queue_items)

	# 关闭服务器
	# manager.shutdown


def task_worker(queue_key='', address=('127.0.0.1', 5000), authkey=b'aabbcc'):
	# 创建消费队列实例
	Manager.register(queue_key)
	worker = Manager(address=address, authkey=authkey)

	# 并连接到任务分发服务器
	log (f'连接到任务分发服务器: {address}')
	worker.connect()

	# 获取任务,并完成消费
	task = getattr(worker, queue_key)()
	while 1:
		if task.empty():
			log('任务列表为空...')
			time.sleep(1)
			continue
		n = task.get(timeout=1)
		log (f'获取到任务: {n}')
		# result.put(n)
		time.sleep(0.1)


if __name__ == '__main__':
	pass
